/**
 * 用户反馈工具库
 */
import { EventEmitter } from 'events';
import { 
  message, 
  notification,
  Modal,
} from 'antd';
import { Icon } from 'antd';

/**
 * feedback组件配置
 */
message.config({ top: 100, duration: 2, maxCount: 1 });
notification.config({ placement: 'bottomRight', bottom: 0, duration: 2 });

/**
 * feedback事件配置
 */
const feedbackEventEmitter = new EventEmitter();

/**
 * message.success()提示
 */
feedbackEventEmitter.addListener('feedback.event.message.success', (content, resolve) => {
  message.success(content).then(resolve);
});

export const messageSuccess = (content) => {
  return new Promise((resolve) => {
    feedbackEventEmitter.emit('feedback.event.message.success', content, resolve);
  });
}

/**
 * message.error()提示
 */
feedbackEventEmitter.addListener('feedback.event.message.error', (content, resolve) => {
  message.error(content).then(resolve);
});

export const messageError = (content) => {
  return new Promise((resolve) => {
    feedbackEventEmitter.emit('feedback.event.message.error', content, resolve);
  });
}

/**
 * notification.success()提示
 */
export const notificationSuccess = () => {

}

export const CONFIRM_MODAL_OK = 'ok';
export const CONFIRM_MODAL_CANCEL = 'cancel';

/**
 * modal.success()提示
 */
feedbackEventEmitter.addListener('feedback.event.modal.success', (content, resolve) => {
  Modal.success({
    // title: '提示',
    content: content,
    okText: '确定',
    okType: 'primary',
    onOk: () => resolve(CONFIRM_MODAL_OK),
  });
});

export const modalSuccess = (content) => {
  return new Promise((resolve) => {
    feedbackEventEmitter.emit('feedback.event.modal.success', content, resolve);
  });
}

/**
 * modal.error()提示
 */
feedbackEventEmitter.addListener('feedback.event.modal.error', (content, resolve) => {
  Modal.error({
    // title: '提示',
    content: content,
    okText: '确定',
    okType: 'primary',
    onOk: () => resolve(),
  });
});

export const modalError = (content) => {
  return new Promise((resolve) => {
    feedbackEventEmitter.emit('feedback.event.modal.error', content, resolve);
  });
}


/**
 * modal.confirm()提示
 */
feedbackEventEmitter.addListener('feedback.event.modal.confirm', (title, content, resolve) => {
  Modal.confirm({
    title: title,
    content: content,
    icon: (<Icon type="exclamation-circle" />),
    okText: "确认",
    cancelText: "取消",
    onCancel: () => resolve(CONFIRM_MODAL_CANCEL),
    onOk: () => resolve(CONFIRM_MODAL_OK),
  });
});

export const modalConfirm = (title, content) => {
  return new Promise((resolve) => {
    feedbackEventEmitter.emit('feedback.event.modal.confirm', title, content, resolve);
  });
}

